//
//  NearDetailViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2018/1/20.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "NearDetailViewController.h"
#import "OrderTableViewCell.h"
#import "LXOrderModel.h"
#import "CancleVC.h"
#import "ShopDetailViewController.h"
#import "PayViewController.h"
#import "LogisticsViewController.h"
#import "SalesViewController.h"

@interface NearDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) LXOrderModel *model;
@property (nonatomic ,strong) NSMutableArray *orderArray;
@property(assign, nonatomic)  NSInteger timeCount;
@property(strong, nonatomic) NSTimer *timer;

@end

@implementation NearDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"订单详情");
    self.orderArray=[[NSMutableArray alloc]init];
    [self getDetaiData];
    
    [self.view addSubview:self.tableView];
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.backgroundColor=Gray_Color;
        _tableView.dataSource=self;
        _tableView.showsHorizontalScrollIndicator=NO;
        _tableView.separatorColor=SeparatorColor;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"OrderTableViewCell"];
    }
    return _tableView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 100;
    }
    return 49;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return self.orderArray.count;
    }else{
        return 3;
    }
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        if ([self.detailDic[@"status"] intValue]==10||[self.detailDic[@"status"] intValue]==11) {
            return 70;
        }else if ([self.detailDic[@"status"] intValue]==300){
            return 120;
        }
        return 60;
    }else{
        return 0.01;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==0) {
        return 120;
    }else if (section==2){
        return 70;
    }else{
        return 10;
    }
    return 185;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        self.model=self.orderArray[indexPath.row];
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTableViewCell"];
        [cell.shopImg sd_setImageWithURL:[NSURL URLWithString:self.model.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        NSString*string =[NSString stringWithFormat:@"%@",self.model.saleName];
        NSArray *array = [string componentsSeparatedByString:@" "];
        cell.shopName.text=array[0];
        cell.shopDetail.text=[NSString stringWithFormat:@"工艺类型:%@",array[1]];
        cell.shopPrice.text=[NSString stringWithFormat:@"¥ %@.00",self.model.price];
        cell.shopNumber.text=[NSString stringWithFormat:@"x %@",self.model.quantity];
        return cell;
    }else{
        static NSString *cellIdentifier=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.textColor=TextColor3;
        cell.textLabel.font=FontAndStyle12;
        cell.detailTextLabel.textColor=TextColor6;
        cell.detailTextLabel.font=FontAndStyle12;
        if (indexPath.section==1) {
            NSDictionary *dic=self.detailDic[@"arrival"];
            if (indexPath.row==0) {
                cell.textLabel.text=@"到店信息";
                cell.detailTextLabel.text=@"";
            }else if(indexPath.row==1){
                cell.textLabel.text=@"到店时间";
                cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",[self timeWithTimeInterval:dic[@"time"]]];
            }else{
                cell.textLabel.text=@"到店人信息";
                cell.detailTextLabel.text=[NSString stringWithFormat:@"%@  %@",dic[@"name"],dic[@"phone"]];
            }
            
        }else{
            NSDictionary *dic=self.detailDic[@"store"];
            if (indexPath.row==0) {
                cell.textLabel.text=@"经销店信息";
                cell.detailTextLabel.text=@"";
            }else if(indexPath.row==1){
                cell.textLabel.text=@"店铺地址";
                cell.detailTextLabel.text=dic[@"area"];
            }else{
                cell.textLabel.text=@"联系电话";
                cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",dic[@"phone"]];
            }
        }
        return cell;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        NSString *s=MyString(@"等待付款");
        CGSize size=[s sizeWithAttributes:@{NSFontAttributeName:FontAndStyle14 }];
        UIView *view=[[UIView alloc]init];
        view.backgroundColor=Gray_Color;
        UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 50)];
        view1.backgroundColor=[UIColor whiteColor];
        [view addSubview:view1];
        MYLabel*orderLabel=[[MYLabel alloc]initWithFrame:CGRectMake(10, 14, MainW-40-size.width, size.height)];
        orderLabel.textColor=TextColor3;
        orderLabel.text=[NSString stringWithFormat:@"%@: %@",MyString(@"订单号"),self.detailDic[@"orderSn"]];
        [view1 addSubview:orderLabel];
        MYLabel *statuelabel=[[MYLabel alloc]initWithFrame:CGRectMake(MainW-size.width-15, 14, size.width+5, size.height)];
        statuelabel.textColor=Red_Color;
        statuelabel.text=MyString(@"等待付款");
        statuelabel.textAlignment=NSTextAlignmentRight;
        [view1 addSubview:statuelabel];
        UILabel *timeLable=[[UILabel alloc]initWithFrame:CGRectMake(0, statuelabel.bsg_bottom+9, MainW-10, 15)];
        timeLable.textAlignment=NSTextAlignmentRight;
        timeLable.font=[UIFont systemFontOfSize:11];
        timeLable.textColor=TextColor6;
        timeLable.hidden=YES;
        timeLable.text=@"30分钟后自动关闭";
        [view1 addSubview:timeLable];
        UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(0, view1.bsg_bottom+1, MainW, 50)];
        view3.backgroundColor=[UIColor whiteColor];
        [view addSubview:view3];
        MYLabel*label=[[MYLabel alloc]initWithFrame:CGRectMake(10, 0, MainW-10, 50)];
        label.textColor=TextColor3;
        label.text=[NSString stringWithFormat:@"%@:%@",MyString(@"取消原因"),@"支付宝余额不足"];
        view3.hidden=YES;
        [view3 addSubview:label];
        switch ([self.detailDic[@"status"] intValue]) {
            case 10:
            {
                view1.frame=CGRectMake(0, 0, MainW, 60);
                timeLable.hidden=NO;
                NSDateFormatter * df = [[NSDateFormatter alloc] init];
                df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
                
                //            NSString * dateString1 = @"2016-03-23 09:00";
                //            NSString * dateString2 = @"2016-03-25 11:00";
                
                NSDate * date1 = [df dateFromString:[self timeWithTimeIntervalString:self.detailDic[@"closeTime"]]];
                NSDate * date2 = [df dateFromString:[self getCurrentTime]];
                
                
                NSTimeInterval time = [date1 timeIntervalSinceDate:date2]; //date1是前一个时间(早)，date2是后一个时间(晚)
                int lasttime=(int)time;
                __block int timeout=lasttime; //倒计时时间
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
                dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
                dispatch_source_set_event_handler(_timer, ^{
                    if(timeout <= 0)
                    { //倒计时结束，关闭
                        dispatch_source_cancel(_timer);
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置
                            [self.navigationController popViewControllerAnimated:YES];
                        });
                    }
                    else
                    {
                        int seconds = timeout;
                        NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置
                            //self.phoneWord.enabled=NO;
                            timeLable.text=[NSString stringWithFormat:@"%@秒后自动关闭",strTime];
                        });
                        timeout--;
                    }
                });
                dispatch_resume(_timer);
                
                //NSLog(@"time = %f", time);
                statuelabel.text=MyString(@"待付款");
            }
                break;
            case 11:
            {
                view1.frame=CGRectMake(0, 0, MainW, 60);
                timeLable.hidden=NO;
                NSDateFormatter * df = [[NSDateFormatter alloc] init];
                df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
                
                //            NSString * dateString1 = @"2016-03-23 09:00";
                //            NSString * dateString2 = @"2016-03-25 11:00";
                
                NSDate * date1 = [df dateFromString:[self timeWithTimeIntervalString:self.detailDic[@"closeTime"]]];
                NSDate * date2 = [df dateFromString:[self getCurrentTime]];
                
                
                NSTimeInterval time = [date1 timeIntervalSinceDate:date2]; //date1是前一个时间(早)，date2是后一个时间(晚)
                int lasttime=(int)time;
                __block int timeout=lasttime; //倒计时时间
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
                dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
                dispatch_source_set_event_handler(_timer, ^{
                    if(timeout <= 0)
                    { //倒计时结束，关闭
                        dispatch_source_cancel(_timer);
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置
                            [self.navigationController popViewControllerAnimated:YES];
                        });
                    }
                    else
                    {
                        int seconds = timeout;
                        NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //设置界面的按钮显示 根据自己需求设置
                            //self.phoneWord.enabled=NO;
                            timeLable.text=[NSString stringWithFormat:@"%@秒后自动关闭",strTime];
                        });
                        timeout--;
                    }
                });
                dispatch_resume(_timer);
                statuelabel.text=MyString(@"待付定金");
            }
                break;
            case 50:
            {
                statuelabel.text=MyString(@"待付尾款");
            }
                break;
            case 35:
            {
                statuelabel.text=MyString(@"待接单");
            }
                break;
            case 45:
            {
                statuelabel.text=MyString(@"待到店");
            }
                break;
            case 80:
            {
                statuelabel.text=MyString(@"待发货");
            }
                break;
            case 90:
            {
                statuelabel.text=MyString(@"待收货");
            }
                break;
            case 200:
            {
                statuelabel.text=MyString(@"已完成");
            }
                break;
            case 300:
            {
                statuelabel.text=MyString(@"交易取消");
                view3.hidden=NO;
                label.text=[NSString stringWithFormat:@"%@:%@",MyString(@"取消的原因"),self.detailDic[@"reason"]];
            }
                break;
            default:
                break;
        }
        
        return view;
    }else{
        return nil;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        self.model=self.orderArray[indexPath.row];
        ShopDetailViewController *vc=[[ShopDetailViewController alloc]init];
        vc.spuId=self.model.skuId;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section==0) {
        UIView *view=[[UIView alloc]init];
        view.backgroundColor=Gray_Color;
        UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, 10, MainW, 105)];
        view1.backgroundColor=[UIColor whiteColor];
        [view addSubview:view1];
        MYLabel *totlePrice=[[MYLabel alloc]initWithFrame:CGRectMake(10, 8, MainW/2-10, 15)];
        totlePrice.textColor=TextColor3;
        totlePrice.text=MyString(@"商品总额");
        [view1 addSubview:totlePrice];
        MYLabel *transport=[[MYLabel alloc]initWithFrame:CGRectMake(10, totlePrice.bsg_bottom+6, MainW/2-10, 15)];
        transport.textColor=TextColor3;
        transport.text=MyString(@"运费");
        [view1 addSubview:transport];
        
        MYLabel *Prince=[[MYLabel alloc]initWithFrame:CGRectMake(MainW/2, 8, MainW/2-10, 15)];
        Prince.textColor=Red_Color;
        Prince.text=[NSString stringWithFormat:@"¥ %@",self.detailDic[@"price"]];
        Prince.textAlignment=NSTextAlignmentRight;
        [view1 addSubview:Prince];
        MYLabel *transportPrice=[[MYLabel alloc]initWithFrame:CGRectMake(MainW/2, Prince.bsg_bottom+6, MainW/2-10, 15)];
        transportPrice.textColor=Red_Color;
        transportPrice.text=[NSString stringWithFormat:@"¥ %@",self.detailDic[@"postFee"]];;
        transportPrice.textAlignment=NSTextAlignmentRight;
        [view1 addSubview:transportPrice];
        UIView *line=[ZJUITools getCutLine];
        line.frame=CGRectMake(10, 53, MainW-20, 0.5);
        [view1 addSubview:line];
        MYLabel *PayPrice=[[MYLabel alloc]init];
        PayPrice.textColor=Red_Color;
        PayPrice.text=[NSString stringWithFormat:@"¥ %@",self.detailDic[@"downPayment"]];
        PayPrice.font=[UIFont systemFontOfSize:15];
        PayPrice.textAlignment=NSTextAlignmentRight;
        [view1 addSubview:PayPrice];
        CGSize size=[PayPrice.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
        PayPrice.frame=CGRectMake(MainW-10-size.width, line.bsg_bottom+10, size.width, 15);
        UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, MainW-10-size.width-5, 13)];
        lable.bsg_bottom=PayPrice.bsg_bottom;
        lable.textColor=Red_Color;
        lable.textAlignment=NSTextAlignmentRight;
        lable.font=FontAndStyle12;
        lable.text=MyString(@"需付款");
        if ([self.detailDic[@"status"] intValue]==11) {
            PayPrice.text=[NSString stringWithFormat:@"¥ %@",self.detailDic[@"downPayment"]];
        }else if ([self.detailDic[@"status"] intValue]==50){
            PayPrice.text=[NSString stringWithFormat:@"¥ %@",self.detailDic[@"finalPayment"]];
        }
        [view1 addSubview:lable];
        
        UILabel *tmlable=[[UILabel alloc]initWithFrame:CGRectMake(0, lable.bsg_bottom+6, MainW-10, 15)];
        tmlable.textColor=TextColor6;
        tmlable.textAlignment=NSTextAlignmentRight;
        tmlable.font=FontAndStyle12;
        tmlable.text=[NSString stringWithFormat:@"%@:%@",MyString(@"创建时间"),[self timeWithTimeIntervalString:self.detailDic[@"createdTime"]]];
        [view1 addSubview:tmlable];
        return view;
    }else if(section==2){
        UIView*view=[[UIView alloc]init];
        view.backgroundColor=Gray_Color;
        UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0, 10, MainW, 49)];
        view2.backgroundColor=[UIColor whiteColor];
        [view addSubview:view2];
        //第一个按钮
        UIButton *btn=[[UIButton alloc]init];
        [btn setTitle:MyString(@"联系客服") forState:UIControlStateNormal];
        [btn setTitleColor:tableBarRedColor forState:UIControlStateNormal];
        btn.layer.borderWidth=0.5;
        btn.layer.borderColor=tableBarRedColor.CGColor;
        ViewRadius(btn, 2);
        btn.tag=0;
        btn.titleLabel.font=FontAndStyle12;
        btn.frame=CGRectMake(MainW-67, 15, 60, 20);
        [btn addTarget:self action:@selector(orderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view2 addSubview:btn];
        //第二个按钮
        UIButton *btn1=[[UIButton alloc]init];
        [btn1 setTitle:MyString(@"联系客服") forState:UIControlStateNormal];
        [btn1 setTitleColor:tableBarRedColor forState:UIControlStateNormal];
        btn1.layer.borderWidth=0.5;
        btn1.layer.borderColor=tableBarRedColor.CGColor;
        ViewRadius(btn1, 2);
        btn1.tag=1;
        btn1.titleLabel.font=FontAndStyle12;
        btn1.frame=CGRectMake(MainW-67-65, 15, 60, 20);
        [btn1 addTarget:self action:@selector(orderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view2 addSubview:btn1];
        //第三个按钮
        UIButton *btn2=[[UIButton alloc]init];
        [btn2 setTitle:MyString(@"联系客服") forState:UIControlStateNormal];
        [btn2 setTitleColor:tableBarRedColor forState:UIControlStateNormal];
        btn2.layer.borderWidth=0.5;
        btn2.layer.borderColor=tableBarRedColor.CGColor;
        ViewRadius(btn2, 2);
        btn2.tag=2;
        btn2.titleLabel.font=FontAndStyle12;
        btn2.frame=CGRectMake(MainW-67-130, 15, 60, 20);
        [btn2 addTarget:self action:@selector(orderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view2 addSubview:btn2];
        
        //第三个按钮
        UIButton *btn3=[[UIButton alloc]init];
        [btn3 setTitle:MyString(@"删除订单") forState:UIControlStateNormal];
        [btn3 setTitleColor:tableBarRedColor forState:UIControlStateNormal];
        btn3.layer.borderWidth=0.5;
        btn3.layer.borderColor=tableBarRedColor.CGColor;
        ViewRadius(btn3, 2);
        btn3.tag=3;
        btn3.hidden=YES;
        btn3.titleLabel.font=FontAndStyle12;
        btn3.frame=CGRectMake(MainW-67-195, 15, 60, 20);
        [btn3 addTarget:self action:@selector(orderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view2 addSubview:btn3];
        
        switch ([self.detailDic[@"status"] intValue]) {
            case 10:
            {
                [btn setTitle:MyString(@"确认付款") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"取消订单") forState:UIControlStateNormal];
                [btn2 setTitle:MyString(@"联系客服") forState:UIControlStateNormal];
            }
                break;
            case 35:
            {
                btn2.hidden=YES;
                [btn setTitle:MyString(@"取消订单") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"联系商家") forState:UIControlStateNormal];
            }
                break;
            case 45:
            {
                [btn setTitle:MyString(@"已到店") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"取消订单") forState:UIControlStateNormal];
                [btn2 setTitle:MyString(@"联系商家") forState:UIControlStateNormal];
            }
                break;
            case 11:
            {
                [btn setTitle:MyString(@"确认付款") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"取消订单") forState:UIControlStateNormal];
                [btn2 setTitle:MyString(@"联系商家") forState:UIControlStateNormal];
            }
                break;
            case 50:
            {
                [btn setTitle:MyString(@"确认付款") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"取消订单") forState:UIControlStateNormal];
                [btn2 setTitle:MyString(@"联系商家") forState:UIControlStateNormal];
            }
                break;
            case 80:
            {
                [btn setTitle:MyString(@"联系客服") forState:UIControlStateNormal];
                btn1.hidden=YES;
                btn2.hidden=YES;
            }
                break;
            case 90:
            {
                [btn setTitle:MyString(@"查看物流") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"确认收货") forState:UIControlStateNormal];
                btn2.hidden=YES;;
            }
                break;
            case 200:
            {
                [btn setTitle:MyString(@"申请售后") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"再次购买") forState:UIControlStateNormal];
                [btn2 setTitle:MyString(@"联系客服") forState:UIControlStateNormal];
                btn3.hidden=NO;
                [btn3 setTitleColor:TextColor6 forState:UIControlStateNormal];
                btn3.layer.borderWidth=0.5;
                btn3.layer.borderColor=TextColor6.CGColor;
            }
                break;
            case 300:
            {
                [btn setTitle:MyString(@"再次购买") forState:UIControlStateNormal];
                [btn1 setTitle:MyString(@"删除订单") forState:UIControlStateNormal];
                [btn1 setTitleColor:TextColor6 forState:UIControlStateNormal];
                btn1.layer.borderWidth=0.5;
                btn1.layer.borderColor=TextColor6.CGColor;
                ViewRadius(btn1, 2);
                btn2.hidden=YES;
            }
                break;
            default:
                break;
        }
        
        return view;
    }else{
        UIView *view=[[UIView alloc]init];
        view.backgroundColor=Gray_Color;
        return view;
    }
}
-(void)orderBtnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 0:
        {
            switch ([self.detailDic[@"status"] intValue]) {
                case 10:
                {
                    [self loadMergeRequest:self.detailDic[@"orderSn"]];
                    
                    //NSLog(@"确认付款");
                }
                    break;
                case 11:
                {
                    [self loadMergeRequest:self.detailDic[@"orderSn"]];
                    
                    //NSLog(@"确认定金");
                }
                    break;
                case 50:
                {
                    [self loadMergeRequest:self.detailDic[@"orderSn"]];
                    
                    //NSLog(@"确认尾款");
                }
                    break;
                case 35:
                {
                    [self cancleClick];
                    
                    //NSLog(@"取消订单");
                }
                    break;
                case 45:
                {
                    [self reachNearClick];
                    
                    //NSLog(@"确认到店");
                }
                    break;
                case 80:
                {
                    EaseMessageViewController*vc=[[EaseMessageViewController alloc]initWithConversationChatter:[NSString stringWithFormat:@"%@",self.detailDic[@"sellerId"]] conversationType:EMConversationTypeChat];
                    vc.title=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerId"]];
                    [self.navigationController pushViewController:vc animated:YES];
                    //[self ContactCustomerService:];
                    //NSLog(@"联系客服");
                }
                    break;
                case 90:
                {
                    self.model=self.orderArray[0];
                    LogisticsViewController*vc=[[LogisticsViewController alloc]init];
                    vc.logisticCode=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerLogistic"][@"logisticCode"]];
                    vc.orderSn=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerLogistic"][@"orderSn"]];
                    vc.shipperCode=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerLogistic"][@"shipperCode"]];
                    vc.shipperName=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerLogistic"][@"shipperName"]];
                    vc.imgUrl=[NSString stringWithFormat:@"%@",self.model.imageUrl];
                    [self.navigationController pushViewController:vc animated:YES];
                    //NSLog(@"查看物流");
                }
                    break;
                case 200:
                {
                    UserOrderModle*modle=[UserOrderModle new];
                    modle.createdTime=self.detailDic[@"createdTime"];
                    modle.orderSn=[NSString stringWithFormat:@"%@",self.detailDic[@"orderSn"]];
                    SalesViewController*vc=[[SalesViewController alloc]init];
                    vc.modle=modle;
                    [self.navigationController pushViewController:vc animated:YES];
                    //NSLog(@"申请售后");
                }
                    break;
                case 300:
                {
                    [self buyAgain:self.detailDic[@"items"]];
                    //NSLog(@"再次购买");
                }
                    break;
                default:
                    break;
            }
            
        }
            break;
        case 1:
        {
            switch ([self.detailDic[@"status"] intValue]) {
                case 10:
                {
                    //取消订单
                    [self cancleClick];
                }
                    break;
                case 11:
                {
                    [self cancleClick];
                    
                }
                    break;
                case 35:
                {
                    //联系商家
                    EaseMessageViewController*vc=[[EaseMessageViewController alloc]initWithConversationChatter:[NSString stringWithFormat:@"%@",self.detailDic[@"sellerId"]] conversationType:EMConversationTypeChat];
                    vc.title=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerId"]];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 45:
                {
                    //取消订单
                    [self cancleClick];
                }
                    break;
                case 50:
                {
                    //取消订单
                    [self cancleClick];
                }
                    break;
                case 80:
                {
                    [self cancleClick];
                }
                    break;
                case 90:
                {
                    [self loadreceiveShop:self.detailDic[@"orderSn"]];
                    //NSLog(@"确认收货");
                }
                    break;
                case 200:
                {
                    //NSLog(@"再次购买");
                    [self buyAgain:self.detailDic[@"items"]];
                }
                    break;
                case 300:
                {
                    LXAlertView *alert=[[LXAlertView alloc]initWithTitle:MyString(@"提示!") message:MyString(@"订单删除") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
                        if (clickIndex==1) {
                            [self deleteOrder:self.detailDic[@"orderSn"]];
                        }
                    }];
                    [alert showLXAlertView];
                }
                    break;
                default:
                    break;
            }
            
        }
            break;
            
        case 2:
        {
            if ([self.detailDic[@"status"] intValue]==10||[self.detailDic[@"status"] intValue]==11||[self.detailDic[@"status"] intValue]==45||
                [self.detailDic[@"status"] intValue]==50) {
                //联系商家
                EaseMessageViewController*vc=[[EaseMessageViewController alloc]initWithConversationChatter:[NSString stringWithFormat:@"%@",self.detailDic[@"sellerId"]] conversationType:EMConversationTypeChat];
                vc.title=[NSString stringWithFormat:@"%@",self.detailDic[@"sellerId"]];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }
            break;
        case 3:
        {
            LXAlertView *alert=[[LXAlertView alloc]initWithTitle:MyString(@"提示!") message:MyString(@"订单删除") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
                if (clickIndex==1) {
                    [self deleteOrder:self.detailDic[@"orderSn"]];
                    //NSLog(@"删除订单");
                }
            }];
            [alert showLXAlertView];
        }
            break;
            
        default:
            break;
    }
}
//合并支付
-(void)loadMergeRequest:(NSString*)ordersn{
    //self.orderModle=self.orderArray[ordersn.tag];
    NSArray *array=@[ordersn];
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    //dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orders"]=[array mj_JSONString];
    [self doHttpRequestWithApi:BATCHPAY method:GET dic:dic successBlock:^(NSDictionary *dic) {
        PayViewController *vc=[[PayViewController alloc]init];
        vc.orderDic=[dic[@"data"] copy];
        vc.orderSn=dic[@"data"][@"payNo"];;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}
//确认到店
-(void)reachNearClick{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=self.detailDic[@"orderSn"];
    [self doHttpRequestWithApi:NearReach method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self getDetaiData];
        
    }];
}
-(void)buyAgain:(NSArray *)array{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"carts"]=[array mj_JSONString];
    [self doHttpRequestWithApi:API_BUYAGAIN method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [MBProgressHUD showSuccess:MyString(@"加入购物车")];
        //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //            MianTabBarViewController *tabbar = [[MianTabBarViewController alloc] init];
        //            [tabbar setSelectedIndex:2];
        //            [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
        //        });
    }];
}
-(void)deleteOrder:(NSString *)ordersn{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=ordersn;
    [self doHttpRequestWithApi:API_REMOVEORDER method:POST dic:dic successBlock:^(NSDictionary *dic) {
        [MBProgressHUD showSuccess:MyString(@"删除成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
}
//确认收货
-(void)loadreceiveShop:(NSString *)orderSn{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=orderSn;
    [self doHttpRequestWithApi:API_RECEIVESHOP method:POST dic:dic successBlock:^(NSDictionary *dic) {
        //[self.tableView reloadData];
        [MBProgressHUD showSuccess:MyString(@"收货成功")];
        [self dismissDialog];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
}
-(NSString*)changeTime:(NSString *)string{
    NSString *str=string;//时间戳
    NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}
- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}
-(void)ContactCustomerService:(NSString *)string{
    
}
-(void)cancleClick{
    CancleVC* transparentView = [[CancleVC alloc] init];
    transparentView.orderSn=self.detailDic[@"orderSn"];
    transparentView.cancleBlock = ^(){
        [self showToast:MyString(@"订单取消")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    };
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        transparentView.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    }else{
        self.modalPresentationStyle=UIModalPresentationCurrentContext;
    }
    
    transparentView.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.01];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self.navigationController presentViewController:transparentView animated:YES completion:^{///弹出控制器
        ///控制器出现后调用
    }];
	
//	[self.navigationController pushViewController:transparentView animated:YES];
	
    //NSLog(@"取消订单");
}
- (NSString *)timeWithTimeInterval:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
-(void)getDetaiData{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=self.orderSn;
    [self doHttpRequestWithApi:API_ORDERDETAIL_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.detailDic=dic[@"data"];
        self.orderArray=[LXOrderModel mj_objectArrayWithKeyValuesArray:self.detailDic[@"items"]];
        [self.tableView reloadData];
        
    }];
}

@end
